package com.zhangdi.demo.date;

import cn.hutool.core.util.StrUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangdi
 * @version 1.0
 * @date 2022.2.11 9:15
 */
class fff{
    Integer projectTime = 123;
}
public class BeanUtils {
    public static void main(String[] args) {
        Object sourceValue = 111d;
        Field[] fields = fff.class.getDeclaredFields();
        for (Field field : fields) {
            if (sourceValue instanceof Number) {
                if (field.getType() == long.class || field.getType() == Long.class) {
                    sourceValue = ((Number) sourceValue).longValue();
                } else if (field.getType() == int.class || field.getType() == Integer.class) {
                    sourceValue = ((Number) sourceValue).intValue();
                } else if (field.getType() == float.class || field.getType() == Float.class) {
                    sourceValue = ((Number) sourceValue).floatValue();
                } else if (field.getType() == byte.class || field.getType() == Byte.class) {
                    sourceValue = ((Number) sourceValue).byteValue();
                } else if (field.getType() == short.class || field.getType() == Short.class) {
                    sourceValue = ((Number) sourceValue).shortValue();
                }

                System.out.println(sourceValue.getClass());
            }
        }
    }

    public static <T, K extends String, V> T MapToBean(Map<String, V> map, Class<T> tClass) {
        T t = null;
        if (map != null && map.size() > 0) {
            try {
                t = tClass.newInstance();
                List<Field> fields = new ArrayList<>();
                getAllField(tClass, fields);
                if (fields.size() > 0) {
                    for (Field field : fields) {
                        field.setAccessible(true);
                        String fileName = field.getName();
                        if ("id".equals(fileName) && map.containsKey("_id")) {
                            field.set(t, map.get("_id"));
                        } else if (map.containsKey(fileName)) {
                            Object sourceValue = map.get(fileName);
                            if(sourceValue == null) {
                                continue;
                            }
                            if (sourceValue instanceof String && StrUtil.isBlank((String) sourceValue)) {
                                continue;
                            }

                            if (sourceValue instanceof Number) {
                                if(field.getType() == long.class || field.getType() == Long.class) {
                                    sourceValue = ((Number) sourceValue).longValue();
                                } else if(field.getType() == Integer.class){
                                    sourceValue = ((Number) sourceValue).intValue();
                                }
                            }
                            field.set(t, sourceValue);
                        }
                    }
                }
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return t;
    }

    public static <K, Tt> List<Map<K, Object>> ListBeanToListMap(List<Tt> list) {
        List<Map<K, Object>> mapList = new ArrayList<>();
        if (null == list || list.size() == 0) {
            return mapList;
        }
        for (Tt tt : list) {
            List<Field> fields = new ArrayList<>();
            getAllField(tt.getClass(), fields);
            if (fields.size() > 0) {
                Map<K, Object> map = new HashMap<>();
                for (Field field : fields) {
                    field.setAccessible(true);
                    try {
                        Object value = field.get(tt);
                        if (value != null) {
                            map.put((K) field.getName(), value);
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }

                if (map.size() > 0) {
                    mapList.add(map);
                }
            }
        }

        return mapList;
    }

    private static void getAllField(Class<?> clazz, List<Field> allFields) {
        if (clazz == null) {
            return;
        }
        Field[] fields = clazz.getDeclaredFields();
        if (fields.length > 0) {
            allFields.addAll(Arrays.asList(fields));
        }

        Class<?> superCLazz = clazz.getSuperclass();
        if (superCLazz != null) {
            getAllField(superCLazz, allFields);
        }
    }
}
